package pide.gui;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import java.io.File;

import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JToolBar;
import javax.swing.UIManager;
import javax.swing.filechooser.FileFilter;

import pide.core.Configuration;
import pide.core.Core;
import debug.DebugMessage;


public class Toolbar extends JFrame {

	
	JButton b_change_workspace = new JButton();
	JButton b_new = new JButton();
	JButton b_open = new JButton();
	JButton b_exit = new JButton();
	JToolBar toolbar = new JToolBar();
	
	String[] extensions = new String[0];
	
	public Toolbar()
	{
		
		Configuration conf = Configuration.getInstance();
		setTitle(conf.get("ide.toolbar.title"));
//		System.out.println(Toolkit.getDefaultToolkit().get);
		extensions = conf.getArray("ide.file.extensions");
		
		b_new.setIcon(UIManager.getIcon("FileChooser.newFolderIcon"));
		b_new.setToolTipText("New");
		b_new.setText("New");
		
		b_open.setIcon(UIManager.getIcon("FileView.directoryIcon"));
		b_open.setToolTipText("Open");
		b_open.setText("Open");
		b_open.addActionListener(new ActionListener() {			
			@Override
			public void actionPerformed(ActionEvent e) {				
				JFileChooser jfc = new JFileChooser(new File(Core.getDefault().WORKSPACE));
				jfc.setFileFilter(new FileFilter() {
					
					@Override
					public String getDescription() {	
						String str = "";
						for(String s:extensions)
						{
							str+="*."+s+",";
						}
						return str.substring(0,str.length()-1);
					}
					
					@Override
					public boolean accept(File arg) {	
						for(String e:extensions)
						{
							if(arg.getName().endsWith(e))
								return true;
						}
						return false;
					}
				});
				
				int res = jfc.showOpenDialog(null);
				if(res==JFileChooser.APPROVE_OPTION)
				{
					DebugMessage.debug("Open : "+jfc.getSelectedFile().getName());
					Core.getDefault().open(jfc.getSelectedFile());
				}
			}
		});
		
		
		b_exit.setText("Exit");
		b_exit.setToolTipText("Exit");
		b_exit.addActionListener(new ActionListener() {			
			@Override
			public void actionPerformed(ActionEvent arg0) {
				Core.getDefault().exit();
			}
		});
		
		b_change_workspace.setText("change workspace");
		b_change_workspace.setToolTipText("change workspace");
		b_change_workspace.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
			Core.getDefault().changeWorkspace();
			}
		});
		
		b_new.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent arg0) {
				Core.getDefault().createFile();
			}
		});
		
		
		toolbar.setOrientation(JToolBar.VERTICAL);
		toolbar.add(b_new);
		toolbar.add(b_open);
		toolbar.add(b_change_workspace);
		toolbar.add(b_exit);
		
		 
		add(toolbar);
		
		setMinimumSize(new Dimension(conf.getInt("ide.toolbar.min_width"), conf.getInt("ide.toolbar.min_height")));
		setVisible(true);
		
		setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
		
		addWindowListener(new WindowListener() {
			
			@Override
			public void windowOpened(WindowEvent arg0) {				
			}
			
			@Override
			public void windowIconified(WindowEvent arg0) {				
			}
			
			@Override
			public void windowDeiconified(WindowEvent arg0) {
			}
			
			@Override
			public void windowDeactivated(WindowEvent arg0) {
			}
			
			@Override
			public void windowClosing(WindowEvent arg0) {
				DebugMessage.debug("Window toolbar event : Closing");
				Core.getDefault().exit();
			}
			
			@Override
			public void windowClosed(WindowEvent arg0) {
			}
			
			@Override
			public void windowActivated(WindowEvent arg0) {
			}
		});
		
	}
	
	
	public static void main(String[] args) {
		
	}
	

}
